<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp"%>
<html>
<head>
	<title>货品管理</title>
	<meta name="decorator" content="default"/>
	<script type="text/javascript">
		$(document).ready(function() {
			//$("#name").focus();
			$("#inputForm").validate({
				submitHandler: function(form){
					$("#categoryId").attr("disabled",false);
					changeAttr();
					loading('正在提交，请稍等...');
					form.submit();
				},
				errorContainer: "#messageBox",
				errorPlacement: function(error, element) {
					$("#messageBox").text("输入有误，请先更正。");
					if (element.is(":checkbox")||element.is(":radio")||element.parent().is(".input-append")){
						error.appendTo(element.parent().parent());
					} else {
						error.insertAfter(element);
					}
				}
			});
		});
		
		function changeAttr(){
			
			 $('#eventSkuList').find('tr').each(function () {
				 	//遍历td获取值
		            $(this).find('td').each(function () {    
		            	$(this).find("select").each(function(){
		            		$(this).attr("disabled",false);
		    			});
		            })
		        })
		}
		
		function getAttr(n){
			
			var sup= $("#supplierId").val();
			if(sup==null||sup==''){
				alertx("供应商不能为空");
				return;
			}
			var prd= n.value;
			if(prd==null||prd==''){
				alertx("货号不能为空");
				return;
			}
			
			window.location.href = "${ctx}/ka/productManage/getAttr?supplierId="+sup+"&productCode="+prd;
		}
		
		//获取供应商下的商品编码
		function getProductCode(n){
			window.location.href = "${ctx}/ka/productManage/getProductCode?supplierId="+n.value
		}
		
	function addRow(list, idx, tpl, row){
			
			$(list).append(Mustache.render(tpl, {
				idx: idx, delBtn: true, row: row
			}));
			$(list+idx).find("select").each(function(){
				$(this).val($(this).attr("data-value"));
			});
			
			$(list+idx).find("input[type='checkbox']").each(function(){
				var ss = $(this).attr("data-value");
				for (var i=0; i<ss.length; i++){
					if($(this).val() == ss[i]){
						$(this).attr("checked","checked");
					}
				}
			});
			
		}
		
		function delRow(obj, prefix){
			var id = $(prefix+"_id");
			var delFlag = $(prefix+"_delFlag");
			if (id.val() == ""){
				$(obj).parent().parent().remove();
			}else if(delFlag.val() == "0"){
				
				$.post("${ctx}/mer/spu/deleteSku",{idFlag:id.val()}, function(result){
				    if(result.code =="0"){
				    	$(obj).parent().parent().remove();
				    }else{
				    	alertx(result.msg);
				    }
				  });
			}else if(delFlag.val() == "1"){
				delFlag.val("0");
				$(obj).html("&times;").attr("title", "删除");
				$(obj).parent().parent().removeClass("error");
			}
		}
		
		//批量新增
		function batchAddRow(){
			
			 var  arr = [];
			 
			 var html = '';    
			 
			//获取tbody id="eventSkuList" 所有值
			 $('#eventSkuList').find('tr').each(function () {
				 	//遍历td获取值
		            $(this).find('td').each(function () {    
		            	$(this).find("select").each(function(){
		            		var a = $(this).val();
		            		html +=a
		    			});
		            })
		            arr.push(html)
		            html='';
		        })
			 
		       //console.log(arr); 
		    var price = $("#price").val();    
		    var salesPrice = $("#salesPrice").val();    
			var data = []
			//遍历选中颜色
			$("#color").find("input[type='checkbox']:checked").each(function(){
				
				var colorId = $(this).val();
				
				//遍历选中尺码
				$("#size").find("input[type='checkbox']:checked").each(function(){
					
					var sizeId = $(this).val();
					var cs =colorId+sizeId;
					//console.log(sizeId);
					//console.log(cs);
					var str = arr.join();
					//console.log(str); 
					if(str.indexOf(cs)==-1){
						//把每个组合放到data数组
						data.push({colorId:colorId,sizeId:sizeId,price:price,salesPrice:salesPrice});
						//console.log(data);
					}
				});
			});
			for (var i=0; i<data.length; i++){
				addRow('#eventSkuList', eventSkuRowIdx, eventSkuTpl,data[i]);
				eventSkuRowIdx = eventSkuRowIdx + 1;
			}
		}
	</script>
</head>
<body>
	<ul class="nav nav-tabs">
		<li><a href="${ctx}/ka/productManage/">货品管理列表</a></li>
		<li class="active"><a href="${ctx}/ka/productManage/form?id=${productManage.id}">货品管理<shiro:hasPermission name="ka:productManage:edit">${not empty productManage.id?'修改':'添加'}</shiro:hasPermission><shiro:lacksPermission name="ka:productManage:edit">查看</shiro:lacksPermission></a></li>
	</ul><br/>
	<form:form id="inputForm" modelAttribute="productManage" action="${ctx}/ka/productManage/save" method="post" class="form-horizontal">
		<form:hidden path="id"/>
		<form:hidden path="price"/>
		<form:hidden path="salesPrice"/>
		<sys:message content="${message}"/>		
		<div class="control-group">
			<label class="control-label">供应商：</label>
			<div class="controls">
				<form:select path="supplierId" class="input-medium required" onchange="getProductCode(this)">
					<form:option value="" label=""/>
					<form:options items="${fns:getSupplier()}" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
				<span>货号： </span>
				<form:select path="productCode" class="input-medium required" onchange="getAttr(this)">
					<form:option value="" label=""/>
					<form:options items="${productManage.listPro }" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>	
		</div>
		<div class="control-group">
			<label class="control-label">品类：</label>
			<div class="controls">
				<form:select path="categoryId" class="input-medium required" disabled="true">
					<form:option value="" label=""/>
					<form:options items="${fns:getCategory() }" itemLabel="label" itemValue="value" htmlEscape="false"/>
				</form:select>
				<span class="help-inline"><font color="red">*</font> </span>
			</div>
		</div>
		<div class="control-group">
				<label class="control-label">尺寸：</label>
				<div class="controls" id="size">
					<form:checkboxes items="${productManage.listSize }" path="" itemLabel="label" itemValue="value"/>
					<span class="help-inline"> </span>
				</div></br>
				<label class="control-label">颜色：</label>
				<div class="controls" id="color">
					<form:checkboxes items="${productManage.listColor }" path="" itemLabel="label" itemValue="value"/>
					<span class="help-inline"> </span>
				</div></br>
				<div class="controls">
					<a id="batchAddRow" href="javascript:" onclick="batchAddRow('#eventSkuList');" class="btn">新增</a>
				</div></br>
				<label class="control-label">属性添加：</label>
				<div class="controls">
					<table id="contentTable" class="table table-striped table-bordered table-condensed">
						<thead>
							<tr>
								<th class="hide"></th>
								<th>颜色</th>
								<th>尺码</th>
								<th>数量</th>
								<th>入库价格</th>
								<th>货架</th>
								<th>备注</th>
							</tr>
						</thead>
						<tbody id="eventSkuList">
						</tbody>
					</table>
					<script type="text/template" id="eventSkuTpl">//<!--
						<tr id="eventSkuList{{idx}}">
							<td class="hide">
								<input id="eventSkuList{{idx}}_delFlag" name="eventSkuList[{{idx}}].delFlag" type="hidden" value="{{row.delFlag}}"/>
								<input id="eventSkuList{{idx}}_id" name="eventSkuList[{{idx}}].id" type="hidden" value="{{row.id}}"/>
								<input id="eventSkuList{{idx}}_salesPrice" name="eventSkuList[{{idx}}].salesPrice" type="hidden" value="{{row.salesPrice}}"/>
							</td>
							<td>
								<select name="eventSkuList[{{idx}}].colorId" data-value="{{row.colorId}}" id="colorId" disabled="true">
									<option value=""></option>
									<c:forEach items="${fns:getColor() }" var="v">
										<option value="${v.value}">${v.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<select name="eventSkuList[{{idx}}].sizeId" data-value="{{row.sizeId}}" id="sizeId" disabled="true">
									<option value=""></option>
									<c:forEach items="${fns:getSize() }" var="v">
										<option value="${v.value}">${v.label}</option>
									</c:forEach>
								</select>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_num" name="eventSkuList[{{idx}}].num"  type="text" value="{{row.num}}" maxlength="5" class="input-small required digits"/>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_price" name="eventSkuList[{{idx}}].price" type="text" value="{{row.price}}" maxlength="7" class="input-small required number"/>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_gridNo" name="eventSkuList[{{idx}}].gridNo" type="text" value="{{row.gridNo}}" maxlength="32" class="input-small"/>
							</td>
							<td>
								<input id="eventSkuList{{idx}}_remarks" name="eventSkuList[{{idx}}].remarks" type="text" value="{{row.remarks}}" maxlength="255" class="input-small"/>
							</td>
						</tr>//-->
					</script>
					<script type="text/javascript">
						var eventSkuRowIdx = 0, eventSkuTpl = $("#eventSkuTpl").html().replace(/(\/\/\<!\-\-)|(\/\/\-\->)/g,"");
						$(document).ready(function() {
							var data = ${fns:toJson(productManage.eventSkuList)};
							for (var i=0; i<data.length; i++){
								addRow('#eventSkuList', eventSkuRowIdx, eventSkuTpl, data[i]);
								eventSkuRowIdx = eventSkuRowIdx + 1;
							}
						});
					</script>
				</div>
			</div>
			<div class="control-group">
				<label class="control-label">入库时间：</label>
				<div class="controls">
					<input name="instockDate" type="text" maxlength="20" class="input-medium Wdate required"
						value="<fmt:formatDate value="${productManage.instockDate}" pattern="yyyy-MM-dd HH:mm:ss"/>"
						onclick="WdatePicker({dateFmt:'yyyy-MM-dd HH:mm:ss',isShowClear:false});"/>
					<span class="help-inline"><font color="red">*</font> </span>
				</div>
			</div>
		<div class="form-actions">
			<shiro:hasPermission name="ka:productManage:edit"><input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存"/>&nbsp;</shiro:hasPermission>
			<input id="btnCancel" class="btn" type="button" value="返 回" onclick="history.go(-1)"/>
		</div>
	</form:form>
</body>
</html>